/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package se.bth.ke.firstfriend.dto;

import java.util.Calendar;
import java.util.Date;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import se.bth.ke.firstfriend.model.dto.TodoInfo;

/**
 *
 * @author Markus Goetz
 */
@XmlRootElement
public class EventInfo {
    private static int INVENTED_DEADLINE_OFFSET = 3;
    private static int MAX_NUMBER_OF_CONTENT_LETTERS = 100;

    private static String DEFAULT_PRIORITY = "timeline-default-priority";
    private static String LOW_PRIORITY = "timeline-low-priority";
    private static String MEDIUM_PRIORITY = "timeline-medium-priority";
    private static String HIGH_PRIORITY = "timeline-high-priority";
    private static String DELAYED = "timeline-delayed-priority";

    @XmlElement
    private String title;
    @XmlElement
    private String description;
    @XmlElement
    private boolean durationEvent;
    @XmlElement
    private Date start;
    @XmlElement
    private Date end;
    @XmlElement
    private String classname;

    public EventInfo() {}

    public EventInfo(TodoInfo todo) {
        title = todo.getArticle().getTitle();
        description = shorten(todo.getArticle().getContent());
        durationEvent = true;
        start = todo.getTodo().getCreatedAt();
        Date deadline = todo.getTodo().getDeadline();
        end = deadline != null ? deadline : inventDeadline();

        switch (todo.getTodo().getPriority()) {
            case 0: classname = LOW_PRIORITY; break;
            case 1: classname = MEDIUM_PRIORITY; break;
            case 2: classname = HIGH_PRIORITY; break;
            default: classname = DEFAULT_PRIORITY; break;
        }

        Date now = new Date();
        if (end.compareTo(start) < 0) {
            Date swap = start;
            classname = DELAYED;
            start = end;
            end = swap;
        } else if (end.compareTo(start) == 0) {
            classname = DELAYED;
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(end);
            calendar.add(Calendar.HOUR, 1);
            end = calendar.getTime();
        } else if (end.compareTo(now) <= 0) {
            classname = DELAYED;
        } 
    }

    private Date inventDeadline() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.DATE, INVENTED_DEADLINE_OFFSET);
        return calendar.getTime();
    }

    private String shorten(String toShorten) {
        if (toShorten.length() < MAX_NUMBER_OF_CONTENT_LETTERS) return toShorten;
        return toShorten.substring(0, MAX_NUMBER_OF_CONTENT_LETTERS - 3) + "...";
    }

    /**
     * @return the title
     */
    public String getTitle() {
        return title;
    }

    /**
     * @return the description
     */
    public String getDescription() {
        return description;
    }

    /**
     * @return the durationEvent
     */
    public boolean isDurationEvent() {
        return durationEvent;
    }

    /**
     * @return the start
     */
    public Date getStart() {
        return start;
    }

    /**
     * @return the end
     */
    public Date getEnd() {
        return end;
    }
}
